package com.universal.schedule.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;
import com.universal.schedule.R;
import com.universal.schedule.db.DatabaseHelper;
import com.universal.schedule.enity.Alarm;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;


public class CalendarFragments  extends Fragment implements OnMonthChangedListener,OnDateSelectedListener{
	private View layout;
	private LayoutInflater mInflater;
	private MaterialCalendarView mCalendarView;
	private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();
	//	private TextView mTv;
	private TextView mTv2;
	private Alarm alarm;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		initUI(inflater, container);
		return layout;
	}

	private void initUI(LayoutInflater inflater, ViewGroup container) {
		if(layout==null)
		{
			mInflater=inflater;
			layout = inflater.inflate(R.layout.fragments_calendar, container, false);
			initUI();
			findview();
			initData();
		}
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Calendar instance2 = Calendar.getInstance();
		CalendarDay date = CalendarDay.from(instance2.get(Calendar.YEAR), instance2.get(Calendar.MONTH),
				instance2.get(Calendar.DAY_OF_MONTH));
		mCalendarView.setSelectedDate(date);
		alarm = getNext();
		if(null!=alarm){
			Calendar c = Calendar.getInstance();
			if(c.get(Calendar.YEAR)==alarm.getAlarmTime().get(Calendar.YEAR)&&
					c.get(Calendar.MONTH)==alarm.getAlarmTime().get(Calendar.MONTH)&&
					c.get(Calendar.DAY_OF_MONTH)==alarm.getAlarmTime().get(Calendar.DAY_OF_MONTH)){
				mTv2.setText(alarm.getAlarmName());
			}else{
				mTv2.setText("今天活动已超时或者无任何活动");
			}
		}else{
			mTv2.setText("今天活动已超时或者无任何活动");
		}
	}

	private void initData() {
		mCalendarView.setSelectionColor(getActivity().getResources().getColor(R.color.main_background));
		Calendar instance2 = Calendar.getInstance();
		CalendarDay date = CalendarDay.from(instance2.get(Calendar.YEAR), instance2.get(Calendar.MONTH),
				instance2.get(Calendar.DAY_OF_MONTH));
		mCalendarView.setSelectedDate(date);
		mCalendarView.state().edit().setCalendarDisplayMode(CalendarMode.MONTHS).commit();
		mCalendarView.setTopbarVisible(true);
		mCalendarView.setOnDateChangedListener(this);
		mCalendarView.setOnMonthChangedListener(this);
//		mTv.setText(getSelectedDatesString());

		alarm = getNext();
		if(null!=alarm){
			Calendar c = Calendar.getInstance();
			if(c.get(Calendar.YEAR)==alarm.getAlarmTime().get(Calendar.YEAR)&&
					c.get(Calendar.MONTH)==alarm.getAlarmTime().get(Calendar.MONTH)&&
					c.get(Calendar.DAY_OF_MONTH)==alarm.getAlarmTime().get(Calendar.DAY_OF_MONTH)){
				mTv2.setText(alarm.getAlarmName());
			}else{
				mTv2.setText("今天活动已超时或者无任何活动");
			}
		}else{
			mTv2.setText("今天活动已超时或者无任何活动");
		}
	}

	private void findview() {
		mCalendarView = (MaterialCalendarView) layout.findViewById(R.id.calendarView);
//		mTv = (TextView) layout.findViewById(R.id.textView);
		mTv2 = (TextView) layout.findViewById(R.id.textView2);
	}

	private void initUI() {

	}


	@Override
	public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
		mCalendarView.invalidateDecorators();//重绘日历
		if(selected){
			if(null!=alarm){
				if(date.getCalendar().get(Calendar.YEAR)==alarm.getAlarmTime().get(Calendar.YEAR)&&
						date.getCalendar().get(Calendar.MONTH)==alarm.getAlarmTime().get(Calendar.MONTH)&&
						date.getCalendar().get(Calendar.DAY_OF_MONTH)==alarm.getAlarmTime().get(Calendar.DAY_OF_MONTH)){
					mTv2.setText(alarm.getAlarmName());
				}else{
					mTv2.setText("今天活动已超时或者无任何活动");
				}
			}else{
				mTv2.setText("今天活动已超时或者无任何活动");
			}
		}
	}

	@Override
	public void onMonthChanged(MaterialCalendarView arg0, CalendarDay date) {

//		mTv.setText(date.getYear() + "年" + (date.getMonth() + 1) + "月"+ (date.getDate().getDay() + 1) + "日");
	}

	private String getSelectedDatesString() {
		CalendarDay date = mCalendarView.getSelectedDate();
		if (date == null) {
			return "No Selection";
		}
		return FORMATTER.format(date.getDate());
	}

	private Alarm getNext(){
		Set<Alarm> alarmQueue = new TreeSet<Alarm>(new Comparator<Alarm>() {
			@Override
			public int compare(Alarm lhs, Alarm rhs) {
				int result = 0;
				long diff = lhs.getAlarmTime().getTimeInMillis() - rhs.getAlarmTime().getTimeInMillis();
				if(diff>0){
					return 1;
				}else if (diff < 0){
					return -1;
				}
				return result;
			}
		});

		List<Alarm> alarms = DatabaseHelper.getAll();

		for(Alarm alarm : alarms){
			if(alarm.getAlarmActive())
				alarmQueue.add(alarm);
		}
		if(alarmQueue.iterator().hasNext()){
			return alarmQueue.iterator().next();
		}else{
			return null;
		}
	}
}
